package day9.homeWork;

//b.写两个线程，一个线程打印 1-52，
// 另一个线程打印A-Z，打印顺序是12A34B…5152Z
public class DemoB {
    public synchronized void printNum() {
        for (int i = 1; i <= 52; i++) {
            System.out.print(i);
            if (i % 2 == 0) {
                try {
                    notify();
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public synchronized void printWord() {
        for (int i = 0; i <= 25; i++) {
            System.out.print((char) (i + 65) + "");
            notify();
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        /*DemoB bb = new DemoB();
        new Thread(() -> {
            bb.printNum();
        }).start();
        new Thread(() -> {
            bb.printWord();
        }).start();*/

        int len = 2;
        for (int i = 0; i < len; i++) {
            new Demo2(i, len).start();
        }
    }
}

class Demo2 extends Thread {
    int i;
    int len;
    static int count;
    int num = 1;
    int charNum = 65;

    public Demo2(int i, int len) {
        this.i = i;
        this.len = len;
    }

    @Override
    public void run() {
        for (int j = 0; j < 26; j++) {
            String name = Thread.currentThread().getName();
            synchronized (Demo2.class) {
                try {
                    for (int i = 0; i < 10; i++) {
                        if (count++ % len == i) {
                            if (i == 0) {
                                //打印数字
                                System.out.println(name + num++);
                                System.out.println(name + num++);
                            } else {
                                //打印字母
                                System.out.println(name + (char) charNum++);
                            }
                            Demo2.class.notifyAll();
                            Demo2.class.wait();
                        } else {
                            j--;
                            Demo2.class.notifyAll();
                            Demo2.class.wait();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
